var searchType=-1;
var style=0;
function checkIp(value) { 

   var ip = value;
   var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;//正则表达式 
    
   if(ip=="")
   {
     value="";
     return [true,"",""];   
   }
   else if(re.test(ip))   
   {   
       if(RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256)
        return [true,"",""];   
   }     
   else
   {
   return [false,"ip格式输入有误!",""];    
   }
}


jQuery("#grid").jqGrid({        
   	url:'trojan_manager.php',
	datatype: "json",
	height: 200,
   	colNames:['记录编号','日期','木马名','开始特征','长度','特征值','发现次数','控制策略','重定向IP','详细描述'],
   	colModel:[
   		{name:'ID',index:'ID', editable:true,editoptions:{readonly:true,size:10},width:50},
   		{name:'DATE',index:'DATE',editable:true, width:100,
   				editoptions:{
			   size:10,
			   dataInit:function(el){ $(el).datepicker({dateFormat:'yy-mm-dd'}); 
			   },
			   defaultValue: function(){ 
			   var currentTime = new Date(); 
			   var month = parseInt(currentTime.getMonth() + 1); 
			   month = month <= 9 ? "0"+month : month; 
			   var day = currentTime.getDate(); 
			   day = day <= 9 ? "0"+day : day; 
			   var year = currentTime.getFullYear(); 
			   return year+"-"+month + "-"+day;
			   } },
	           formoptions:{ rowpos:2,label: "上报时间", elmprefix:"(*)",elmsuffix:" yyyy-mm-dd" },
			   editrules:{required:true}},
		{name:'NAME',index:'NAME',editable:true, width:100,formoptions:{ rowpos:3,label:"名字", elmprefix:"(*)"},editrules:{required:true}},
		{name:'BEGIN',index:'BEGIN',editable:true, width:100,formoptions:{ rowpos:4,label:"开始特征", elmprefix:"(*)"},editrules:{required:true}},
		{name:'LEN',index:'LEN',editable:true, width:100,formoptions:{ rowpos:5,label:"长度", elmprefix:"(*)"},editrules:{required:true}},
   		{name:'FEATURE',index:'FEATURE',editable:true, width:100,formoptions:{ rowpos:6, label: "特征",elmprefix:"(*)"},editrules:{required:true}},
   		 {name:'NUM',index:'NUM', editable:false,editoptions:{readonly:true,size:10},width:100},
	   {name:'DEAL',index:'DEAL',editable:true, width:100,	
	          edittype:"select", 	   
		       editoptions:{value:"0:阻断;1:隔离;2:重定向;3:欺骗"},
			    formoptions:{ rowpos:8,elmprefix:"&nbsp;&nbsp;&nbsp;&nbsp;" },
              editrules:{required:false},hidden:true},
	   {name:'REIP',index:'REIP', editable:true,width:100,formoptions:{ rowpos:9, label: "重定向ip名称",elmprefix:"   "},editrules:{required:false},hidden:true},
	   {name:'DETAIL',index:'DETAIL',editable:true, width:150,formoptions:{ rowpos:10, label: "详细信息", elmprefix:" "},editrules:{required:false}}
   	],
    width:"100%",
    rowNum:20,
    rowList:[10,20,30],
   	 mtype: "POST",
   	 pager: jQuery('#pgrid'),
   	 sortname: 'ID',
   	 viewrecords: true,
   	 multiselect:true,
   	 sortable: true,
    sortorder: "asc",
	 editurl:"trojan_manager.php?style=2"
});


$("#edit").click(function(){ var gr = jQuery("#grid").jqGrid('getGridParam','selrow'); 
if( gr != null ) jQuery("#grid").jqGrid('editGridRow',gr,{height:280,reloadAfterSubmit:true}); 
else alert("Please Select Row"); });




$("#del").click(function(){ 
var gr = jQuery("#grid").jqGrid('getGridParam','selarrrow');
var ids=new Array();
if( gr != null ){
var len = gr.length;
for (var i=0;i<len;i++)
{
var ret = jQuery("#grid").jqGrid('getRowData',gr[i]);
ids[i]=ret.ID;
}

jQuery("#grid").jqGrid('delGridRow',ids,{reloadAfterSubmit:true});
}
 
else alert("Please Select Row to delete!"); 
});

//functions 
function add()
{


    style=1;
    $( "#time" ).datepicker( "option", "dateFormat", "yy-mm-dd");
	 var time_mask = jQuery("#time").val();
	 var nm_mask=jQuery("#name").val();
	 var be_mask=jQuery("#begin").val();
	 var len_mask=jQuery("#len").val();
	 var ch_mask=jQuery("#feature").val();
    var deal_mask = jQuery("#deal").val();
    var ip_mask = jQuery("#reip").val();
    var detail_mask = jQuery("#detail").val();
    var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
    var reg=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
    var flag1=0;
    if (re.test(ip_mask))
    {
       if(RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256)
           flag1=1;  
     }
    else{
          if (ip_mask=="")
            flag1=1;
    }

   if ((deal_mask==2 && ip_mask=="")||(nm_mask==""))
    {
         alert("请正确输入");
    }
    else if (flag1==0)
    {
         alert("请输入正确格式");
     }
    else
    {
     if(deal_mask!=2){
     ip_mask="";
      }
    try{
    jQuery("#grid").jqGrid('setGridParam',{url:"trojan_manager.php?q=1&time_mask="+time_mask+"&ch_mask="+ch_mask+"&deal_mask="+deal_mask+"&ip_mask="+ip_mask+"&len_mask="+len_mask+"&be_mask="+be_mask+"&nm_mask="+nm_mask+"&detail_mask="+detail_mask+"&style="+style,page:1}).trigger('reloadGrid');
    jQuery("#grid").jqGrid('setGridParam',{url:"trojan_manager.php"}).trigger('reloadGrid');
    }
    catch(err)
    {
	jQuery("#grid").jqGrid('setGridParam',{url:"trojan_manager.php"}).trigger('reloadGrid'); 
   }
   }
}
function complexSearch(){
   	$( "#s" ).dialog( { 
	       width:350,  
          height:250, 
	       buttons: { 
	      "取消": function() { $(this).dialog("close"); },
	      "重置": function(){
	      jQuery("#reip1").attr("value","");
	      jQuery("#num1").attr("value","");
	      jQuery("#from1").attr("value","");
	      jQuery("#to1").attr("value","");
	      jQuery("#deal1").attr("value","");
	      jQuery("#name1").attr("value","");
	      jQuery("#begin1").attr("value","");
	      jQuery("#len1").attr("value","");
	      jQuery("#feature1").attr("value","");
	      
	      },
	      "提交":function(){

	           style=0
	           var ip_mask = jQuery("#reip1").val();
	           var ch_mask=jQuery("#feature1").val();
	           var nm_mask=jQuery("#name1").val();
	            $( "#from1" ).datepicker( "option", "dateFormat", "yy-mm-dd");
	            $( "#to1" ).datepicker( "option", "dateFormat", "yy-mm-dd");
              var from_mask = jQuery("#from1").val();
	           var to_mask = jQuery("#to1").val();
	           var deal_mask = jQuery("#deal1").val();
	           var len_mask = jQuery("#len1").val();
	           var be_mask = jQuery("#begin1").val();
	           var num_mask = jQuery("#num1").val();
	           jQuery("#grid").jqGrid('setGridParam',{url:"trojan_manager.php?q=1&from_mask="+from_mask+"&to_mask="+to_mask+"&ip_mask="+ip_mask+"&ch_mask="+ch_mask+"&deal_mask="+deal_mask+"&len_mask="+len_mask+"&num_mask="+num_mask+"&be_mask="+be_mask+"&nm_mask="+nm_mask+"&style="+style,page:1}).trigger('reloadGrid'); 
	            jQuery("#grid").jqGrid('setGridParam',{url:"trojan_manager.php"}).trigger('reloadGrid'); 
	         }
	         },
	       close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			} });
   

}
function uploadFile()
{
    
	$( "#file" ).dialog( { 
	       width:350,  
          height:250, 
	       buttons: { 
	      "取消": function() { $(this).dialog("close");style=0; },
	      "提交":function(){
	            style=0;
	            jQuery("#grid").jqGrid('setCaption',"详细: ") .trigger('reloadGrid'); 
	         }
	         },
	       close: function() {
	           style=0;
				allFields.val( "" ).removeClass( "ui-state-error" );
			} });
}










